package reflection;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.Properties;

/**
 * @author DuJiabao
 * @create 2021-01-12 20:42
 */
public class PropertiesTest {
    @Test
    public void test() throws Exception {
//        读取properties文件参数
        Properties props = new Properties();
//        方式一：利用文件流
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("src/classes/reflection/jdbc.properties");
            props.load(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Class<?> aClass = Class.forName(props.getProperty("class"));
        //实例对象一
        Constructor<?> constructor = aClass.getConstructor(int.class, String.class);
        Object o = constructor.newInstance(Integer.parseInt(props.getProperty("age")), props.getProperty("name"));
        System.out.println(o);
        //实例对象二
        Object o1 = aClass.newInstance();
//      方法二：利用ClassLoader
        ClassLoader classLoader = PropertiesTest.class.getClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("reflection/jdbc.properties");
        props.load(resourceAsStream);


    }
}
